package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.widget.ExpandableListView;
import qlsl.androiddesign.activity.subactivity.CategoryListActivity;
import qlsl.androiddesign.activity.subactivity.CopyActivity;
import qlsl.androiddesign.activity.subactivity.FirstCategoryActivity;
import qlsl.androiddesign.adapter.baseadapter.BaseExpandableListAdapter;
import qlsl.androiddesign.adapter.commonadapter.ShoppingExpAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.ShoopChildList;
import qlsl.androiddesign.entity.otherentity.Category.RowsBean;
import qlsl.androiddesign.http.service.subservice.ShoppingService;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.commonview.WrapExpandableListView;

/**
 * 一级分类页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class FirstCategoryView extends FunctionView<FirstCategoryActivity> {

	private ExpandableListView listView;

	private List<RowsBean> list = new ArrayList<RowsBean>();

	private ArrayList<HashMap<String, RowsBean>> groupList = new ArrayList<HashMap<String, RowsBean>>();

	private ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>> childList = new ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>>();

	private Pager pager;

	public FirstCategoryView(FirstCategoryActivity activity) {
		super(activity);
		setContentView(R.layout.activity_first_category);
	}

	protected void initView(View view) {
		setTitle("分类");
		listView = findViewById(R.id.expandableListView);
	}

	protected void initData() {
		ShoppingService.queryCatagoryList(this, activity);

	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			ArrayList<HashMap<String, RowsBean>> groupListResult = (ArrayList<HashMap<String, RowsBean>>) map
					.get("groupList");
			ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>> childListResult = (ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>>) map
					.get("childList");
			pager = (Pager) map.get("pager");

			if (pager.getPageNo() == 1) {
				resetList(groupListResult, childListResult);
			} else {
				groupList.addAll(groupListResult);
				// childList.addAll(childListResult);
			}
			notifyDataSetChanged();
		}
	}

	@SuppressWarnings("unchecked")
	public <T> void showDataChild(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];

			ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>> childListResult = (ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>>) map
					.get("childList");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				childList.clear();
				childList.addAll(childListResult);
			}
			notifyDataSetChanged();
		}
	}

	private void doClickGroupView(View view) {
		int position = listView.getPositionForView(view);
		long flatPostion = listView.getExpandableListPosition(position);
		int groupPosition = ExpandableListView
				.getPackedPositionGroup(flatPostion);
		RowsBean item = groupList.get(groupPosition).get("group");

		int childSize = childList.get(groupPosition).size();
		if (childSize == 0) {
			ShoppingService.queryCatagoryListChild(listView,
					childList.get(groupPosition), item.getSupplierid(), 0,
					this, activity);
		} else {
			/*
			 * if(listView.isGroupExpanded(groupPosition)){
			 * listView.collapseGroup(groupPosition); }else{
			 * listView.expandGroup(groupPosition); }
			 */
			showToast("已经有数据了");
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.layout_child:
			doClickChildView(view);
			break;
		case R.id.layout_group:
			doClickGroupView(view);
			break;
		}
	}

	private void doClickChildView(View view) {

		int position = listView.getPositionForView(view);
		long flatPostion = listView.getExpandableListPosition(position);
		int groupPosition = ExpandableListView
				.getPackedPositionGroup(flatPostion);
		int childPosition = ExpandableListView
				.getPackedPositionChild(flatPostion);
		ShoopChildList.RowsBean item = childList.get(groupPosition)
				.get(childPosition).get("child");
		if (item != null) {
			startActivity(item, CategoryListActivity.class);
		}

		// ShoppingService.queryCatagoryList(null, 0, this, fragment);
	}

	private void resetList(
			ArrayList<HashMap<String, RowsBean>> groupListResult,
			ArrayList<ArrayList<HashMap<String, ShoopChildList.RowsBean>>> childListResult) {
		groupList.clear();
		groupList.addAll(groupListResult);
		childList.clear();
		childList.addAll(childListResult);
	}

	public void notifyDataSetChanged() {
		BaseExpandableListAdapter adapter = (BaseExpandableListAdapter) listView
				.getExpandableListAdapter();
		if (adapter == null) {
			adapter = new ShoppingExpAdapter(activity, listView, groupList,
					childList);
			listView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		adapter.expandGroup();
	}

}
